package com.my.yyr;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;


@SpringBootApplication
@MapperScan("com.my.yyr.dao")//扫描映射器
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){

        return new WebMvcConfigurer() {

            //设置允许接受跨域请求
            @Override
            public void addCorsMappings(CorsRegistry registry) {

                registry.addMapping("/**")//允许对任何路径进行跨域请求
                        .allowedMethods("*")//允许任何跨域请求方式
                        .allowCredentials(true);//允许跨域请求使用session

            }

            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addRedirectViewController("/","/index.html");
            }

            @Override
            public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
                stringHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
                converters.add(0,stringHttpMessageConverter);

                FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
                fastJsonHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
                converters.add(1,fastJsonHttpMessageConverter);
            }
        };

    }

}
